package kaalanshare;

import java.io.IOException;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created with IntelliJ IDEA.
 * User: twin
 * Date: 23.04.12
 * Time: 14:38
 * To change this template use File | Settings | File Templates.
 */

// Server for incoming connections on port 1992
public class ServerRunnable implements Runnable {
    volatile boolean stop = false;
    volatile ServerSocket servsock = null;

    @Override
    public void run() {

        try {
            servsock = new ServerSocket(1992);
            while (!stop) {
                System.out.println("Waiting...");
                Socket sock = null;
                if (servsock != null) {
                    try {
                        Socket clientSocket = servsock.accept();
                        new Thread(new SenderFileRunnable(clientSocket)).start();
                    } catch (IOException e1) {
                        System.out.println("stopping");
                        // e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                    }
                }
                System.out.println("Accepted connection : " + sock);
            }
        } catch (BindException bexp) {
            System.out.println("Address already in use");
        } catch (IOException e1) {
            e1.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
